package com.my.mvc.dao;

import com.my.mvc.db.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.List;

/**
 * 封装了基本的CRUD的方法，以供子类继承使用
 * <p>
 * Created by YJH on 2017/6/26 20:43.
 */
public class DAO<T> {

    private Class<T> clazz;

    private QueryRunner queryRunner = new QueryRunner();

    public DAO() {
        Type superClass = getClass().getGenericSuperclass();

        if (superClass instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) superClass;

            Type[] typeArr = parameterizedType.getActualTypeArguments();
            if (typeArr != null && typeArr.length > 0) {
                if (typeArr[0] instanceof Class) {
                    clazz = (Class<T>) typeArr[0];
                }
            }
        }
    }

    /**
     * 返回某一个字段的值：例如返回一条记录customerName，或返回数据表中有多少条记录
     *
     * @param sql  sql语句
     * @param args 填充sql语句的占位符
     * @param <E>
     * @return
     */
    public <E> E getForValue(String sql, Object... args) {
        Connection connection = null;

        try {
            connection = JdbcUtils.getConnection();
            return (E) queryRunner.query(connection, sql, new ScalarHandler(), args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.releaseConnection(connection);
        }
        return null;
    }

    public List<T> getForList(String sql, Object... args) {
        Connection connection = null;

        try {
            connection = JdbcUtils.getConnection();
            return queryRunner.query(connection, sql, new BeanListHandler<>(clazz), args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.releaseConnection(connection);
        }
        return null;
    }

    public T get(String sql, Object... args) {
        Connection connection = null;

        try {
            connection = JdbcUtils.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(clazz), args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.releaseConnection(connection);
        }
        return null;
    }

    /**
     * 该方法封装了INSERT、DELETE、UPDATE
     *
     * @param sql  sql语句
     * @param args 填充sql语句的占位符
     */
    public void update(String sql, Object... args) {
        Connection connection = null;

        try {
            connection = JdbcUtils.getConnection();
            queryRunner.update(connection, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.releaseConnection(connection);
        }
    }

}
